
Public Class BloodAbnormalStatusHistory

    Protected _year As String
    Protected _police As New Police()
    Protected _blood_abnormal As New BloodAbnormal()

    Public Property police() As Police
        Get
            Return _police
        End Get
        Set(ByVal value As Police)
            _police = value
        End Set
    End Property

    Public Property year() As String
        Get
            Return _year
        End Get
        Set(ByVal value As String)
            _year = value
        End Set
    End Property

    Public Property blood_abnormal() As BloodAbnormal
        Get
            Return _blood_abnormal
        End Get
        Set(ByVal value As BloodAbnormal)
            _blood_abnormal = value
        End Set
    End Property

    Public Sub deletePoliceBloodResultAbnormalStatus(ByVal inBloodResultHistory As BloodResultHistory)
        Try
            Dim decryptText As New DateUtilities.Encrypt_Data()
            Dim objConn As New MySql.Data.MySqlClient.MySqlConnection( _
                decryptText.getDecrypt(System.Configuration.ConfigurationManager.ConnectionStrings( _
                "PoliceHealthConnectionString").ToString(), True))
            Dim objCmd As New MySql.Data.MySqlClient.MySqlCommand()
            objCmd.Connection = objConn
            objCmd.CommandType = CommandType.StoredProcedure
            objCmd.CommandText = "proc_DELETE_PoliceBloodAbnormalStatusHistory"

            Dim objParam As New MySql.Data.MySqlClient.MySqlParameter("inCitizenID", _
                Utilities.ConvertCitizenID(inBloodResultHistory.police.citizen_id))
            objCmd.Parameters.Add(objParam)

            objParam = New MySql.Data.MySqlClient.MySqlParameter("inYear", inBloodResultHistory.year)
            objCmd.Parameters.Add(objParam)

            objConn.Open()
            objCmd.ExecuteNonQuery()
            objConn.Close()

        Catch ex As Exception
            Throw ex
        End Try
    End Sub

    Public Function insertPoliceBloodAbnormalStatusHistory(ByVal inBloodResultHistory As BloodResultHistory)
        Dim blResultStatus As Boolean = True
        For i As Integer = 1 To 3
            blResultStatus = blResultStatus And insertPoliceBloodAbnormalStatusHistory(inBloodResultHistory, i)
        Next
        Return blResultStatus
    End Function


    Public Function insertPoliceBloodAbnormalStatusHistory(ByVal inBloodResultHistory As BloodResultHistory, _
                                                                                    ByVal inLoop As Short) As Boolean
        Try
            Dim decryptText As New DateUtilities.Encrypt_Data()
            Dim objConn As New MySql.Data.MySqlClient.MySqlConnection( _
                decryptText.getDecrypt(System.Configuration.ConfigurationManager.ConnectionStrings( _
                "PoliceHealthConnectionString").ToString(), True))
            Dim objCmd As New MySql.Data.MySqlClient.MySqlCommand()
            objCmd.Connection = objConn
            objCmd.CommandType = CommandType.StoredProcedure
            objCmd.CommandText = "proc_INSERT_PoliceBloodAbnormalStatusHistory"

            Dim objParam As New MySql.Data.MySqlClient.MySqlParameter("inCitizenID", _
                Utilities.ConvertCitizenID(inBloodResultHistory.police.citizen_id))
            objCmd.Parameters.Add(objParam)

            objParam = New MySql.Data.MySqlClient.MySqlParameter("inYear", inBloodResultHistory.year)
            objCmd.Parameters.Add(objParam)

            objParam = New MySql.Data.MySqlClient.MySqlParameter("inOrganizationID", _
                inBloodResultHistory.police.organization.organization_id)
            objCmd.Parameters.Add(objParam)

            objParam = New MySql.Data.MySqlClient.MySqlParameter("inOrganizationNameTha", _
                inBloodResultHistory.police.organization.organization_name_tha)
            objCmd.Parameters.Add(objParam)

            objParam = New MySql.Data.MySqlClient.MySqlParameter("inCreateUser", inBloodResultHistory.police.create_user)
            objCmd.Parameters.Add(objParam)

            Select Case inLoop
                Case 1
                    Select Case inBloodResultHistory.police.gender
                        Case "F"
                            If inBloodResultHistory.hb < 12 And (Not IsDBNull(Utilities.SendToDatabase(inBloodResultHistory.hb))) Then
                                objParam = New MySql.Data.MySqlClient.MySqlParameter("inBloodAbnormalID", 1)
                                objCmd.Parameters.Add(objParam)
                                objConn.Open()
                                objCmd.ExecuteNonQuery()
                                objConn.Close()
                                Return False
                            ElseIf inBloodResultHistory.hct < 36 And (Not IsDBNull(Utilities.SendToDatabase(inBloodResultHistory.hct))) Then
                                objParam = New MySql.Data.MySqlClient.MySqlParameter("inBloodAbnormalID", 1)
                                objCmd.Parameters.Add(objParam)
                                objConn.Open()
                                objCmd.ExecuteNonQuery()
                                objConn.Close()
                                Return False
                            End If
                        Case "M"
                            If inBloodResultHistory.hb < 13 And (Not IsDBNull(Utilities.SendToDatabase(inBloodResultHistory.hb))) Then
                                objParam = New MySql.Data.MySqlClient.MySqlParameter("inBloodAbnormalID", 1)
                                objCmd.Parameters.Add(objParam)
                                objConn.Open()
                                objCmd.ExecuteNonQuery()
                                objConn.Close()
                                Return False
                            ElseIf inBloodResultHistory.hct < 39 And (Not IsDBNull(Utilities.SendToDatabase(inBloodResultHistory.hct))) Then
                                objParam = New MySql.Data.MySqlClient.MySqlParameter("inBloodAbnormalID", 1)
                                objCmd.Parameters.Add(objParam)
                                objConn.Open()
                                objCmd.ExecuteNonQuery()
                                objConn.Close()
                                Return False
                            End If
                    End Select
                        'If inBloodResultHistory.hb < 12 And (Not IsDBNull(Utilities.SendToDatabase(inBloodResultHistory.hb))) Then
                        'objParam = New MySql.Data.MySqlClient.MySqlParameter("inBloodAbnormalID", 1)
                        'objCmd.Parameters.Add(objParam)

                        'objConn.Open()
                        'objCmd.ExecuteNonQuery()
                        'objConn.Close()

                        'Return False
                        'ElseIf inBloodResultHistory.police.gender = "F" Then
                        'If inBloodResultHistory.hct < 36 And _
                        '(Not IsDBNull(Utilities.SendToDatabase(inBloodResultHistory.hct))) Then
                        '    objParam = New MySql.Data.MySqlClient.MySqlParameter("inBloodAbnormalID", 1)
                        '    objCmd.Parameters.Add(objParam)

                        '    objConn.Open()
                        '    objCmd.ExecuteNonQuery()
                        '    objConn.Close()

                        '    Return False
                        'End If
                        'ElseIf inBloodResultHistory.police.gender = "M" Then
                        'If inBloodResultHistory.hct < 40 And _
                        '(Not IsDBNull(Utilities.SendToDatabase(inBloodResultHistory.hct))) Then
                        '    objParam = New MySql.Data.MySqlClient.MySqlParameter("inBloodAbnormalID", 1)
                        '    objCmd.Parameters.Add(objParam)

                        '    objConn.Open()
                        '    objCmd.ExecuteNonQuery()
                        '    objConn.Close()

                        '    Return False
                        'End If
                        'End If
                Case 2
                        If (inBloodResultHistory.wbc < 5000 Or inBloodResultHistory.wbc > 10000) And _
                        (Not IsDBNull(Utilities.SendToDatabase(inBloodResultHistory.wbc))) Then
                            objParam = New MySql.Data.MySqlClient.MySqlParameter("inBloodAbnormalID", 2)
                            objCmd.Parameters.Add(objParam)

                            objConn.Open()
                            objCmd.ExecuteNonQuery()
                            objConn.Close()

                            Return False
                        End If
                Case 3
                        If (inBloodResultHistory.plt < 100000 Or inBloodResultHistory.plt > 400000) And _
                        (Not IsDBNull(Utilities.SendToDatabase(inBloodResultHistory.plt))) Then
                            objParam = New MySql.Data.MySqlClient.MySqlParameter("inBloodAbnormalID", 3)
                            objCmd.Parameters.Add(objParam)

                            objConn.Open()
                            objCmd.ExecuteNonQuery()
                            objConn.Close()

                            Return False
                        End If
            End Select
        Catch ex As Exception
            Dim msgWarnning As New ManageBloodResultHistoryControl()
            msgWarnning.showInsertBloodAbnormalStatusHistoryError()
        End Try
        Return True
    End Function

End Class
